package com.umeng.analytics.pro;

import android.content.Context;
import android.text.TextUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ad {
    public class a {
        private Context a;
        private Set b;

        public a(Context arg2) {

            this.b = new HashSet();
            this.a = arg2;
        }

        public void a() {
            if (!this.b.isEmpty()) {
                StringBuilder v1 = new StringBuilder();
                Iterator v2 = this.b.iterator();
                while (v2.hasNext()) {
                    v1.append(v2.next());
                    v1.append(',');
                }

                v1.deleteCharAt(v1.length() - 1);
                ba.a(this.a).edit().putString("invld_id", v1.toString()).commit();
            }
        }

        public boolean a(String arg2) {
            boolean v0 = !this.b.contains(arg2) ? true : false;
            return v0;
        }

        public void b() {
            String v0 = ba.a(this.a).getString("invld_id", null);
            if (!TextUtils.isEmpty(((CharSequence) v0))) {
                String[] v1 = v0.split(",");
                if (v1 != null) {
                    int v2 = v1.length;
                    int v0_1;
                    for (v0_1 = 0; v0_1 < v2; ++v0_1) {
                        String v3 = v1[v0_1];
                        if (!TextUtils.isEmpty(((CharSequence) v3))) {
                            this.b.add(v3);
                        }
                    }
                }
            }
        }

        public void b(String arg2) {
            this.b.add(arg2);
        }

        public void c(String arg2) {
            this.b.remove(arg2);
        }
    }

    public static ad a;
    private final String b;
    private File c;
    private bm d;
    private long e;
    private long f;
    private Set g;
    private a h;

    ad(Context arg4) {

        this.b = "umeng_it.cache";
        this.d = null;
        this.g = new HashSet();
        this.h = null;
        this.c = new File(arg4.getFilesDir(), "umeng_it.cache");
        this.f = 86400000;
        this.h = new a(arg4);
        this.h.b();
    }

    public static ad a(Context arg4) {
        ad v0_1;
        Class v1 = ad.class;
        synchronized (v1) {
            if (ad.a == null) {
                v0_1 = new ad(arg4);
                ad.a = v0_1;
                v0_1.a(new ae(arg4));
                ad.a.a(new z(arg4));
                ad.a.a(new am(arg4));
                ad.a.a(new ac(arg4));
                ad.a.a(new ab(arg4));
                ad.a.a(new ag(arg4));
                ad.a.a(new aj());
                ad.a.a(new an(arg4));
                al v0_2 = new al(arg4);
                if (!TextUtils.isEmpty(v0_2.f())) {
                    ad.a.a(((y) v0_2));
                }

                ai v0_3 = new ai(arg4);
                if (v0_3.g()) {
                    ad.a.a(((y) v0_3));
                    ad.a.a(new ah(arg4));
                    v0_3.i();
                }

                ad.a.e();
            }

            v0_1 = ad.a;
        }
        return v0_1;
    }

    public boolean a(y arg3) {
        boolean v0 = this.h.a(arg3.b()) ? this.g.add(arg3) : false;
        return v0;
    }

    private void a(bm arg3) {
        byte[] v0_2;
        if (arg3 != null) {
            try {
                synchronized (this) {
                    v0_2 = new cp().a(((cg) arg3));
                }
                if (v0_2 == null) {
                    return;
                }
                bw.a(this.c, v0_2);
            } catch (Throwable v0_1) {
                v0_1.printStackTrace();
            }
        }
    }

    public void a() {
        long v2 = System.currentTimeMillis();
        if (v2 - this.e >= this.f) {
            Iterator v4 = this.g.iterator();
            int v1;
            for (v1 = 0; v4.hasNext(); v1 = v1) {
                Object v0 = v4.next();
                if (!((y) v0).c()) {
                    continue;
                }

                if (((y) v0).a()) {
                    v1 = 1;
                    if (!((y) v0).c()) {
                        this.h.b(((y) v0).b());
                    }
                }
            }

            if (v1 != 0) {
                this.g();
                this.h.a();
                this.f();
            }

            this.e = v2;
        }
    }

    public void a(long arg2) {
        this.f = arg2;
    }

    public bm b() {
        return this.d;
    }

    public String c() {
        return null;
    }

    public void d() {
        int v0_1 = 0;
        Iterator v3 = this.g.iterator();
        int v1;
        for (v1 = 0; v3.hasNext(); v1 = v0_1) {
            Object v0 = v3.next();
            if (!((y) v0).c()) {
                continue;
            }

            if (((y) v0).e() == null || (((y) v0).e().isEmpty())) {
                v0_1 = v1;
            } else {
                ((y) v0).a((List<bk>) null);
                v0_1 = 1;
            }
        }

        if (v1 != 0) {
            this.d.b(false);
            this.f();
        }
    }

    public void e() {
        bm v0 = this.h();
        if (v0 != null) {
            ArrayList v1 = new ArrayList(this.g.size());
            synchronized (this) {
                this.d = v0;
                Iterator v2 = this.g.iterator();
                while (v2.hasNext()) {
                    Object v0_2 = v2.next();
                    ((y) v0_2).a(this.d);
                    if (((y) v0_2).c()) {
                        continue;
                    }

                    ((List) v1).add(v0_2);
                }

                Iterator v1_1 = ((List) v1).iterator();
                while (v1_1.hasNext()) {
                    this.g.remove(v1_1.next());
                }
            }
            this.g();
        }
        return;
    }

    public void f() {
        if (this.d != null) {
            this.a(this.d);
        }
    }

    private void g() {
        bm v1 = new bm();
        HashMap v2 = new HashMap();
        ArrayList v3 = new ArrayList();
        Iterator v4 = this.g.iterator();
        while (v4.hasNext()) {
            Object v0 = v4.next();
            if (!((y) v0).c()) {
                continue;
            }

            if (((y) v0).d() != null) {
                ((Map) v2).put(((y) v0).b(), ((y) v0).d());
            }

            if (((y) v0).e() == null) {
                continue;
            }

            if (((y) v0).e().isEmpty()) {
                continue;
            }

            ((List) v3).addAll(((y) v0).e());
        }

        v1.a(((List) v3));
        v1.a(((Map) v2));
        synchronized (this) {
            this.d = v1;
        }
    }

    private bm h() {
        bm v1_2 = null;
        InputStream v2_1;
        Throwable v0_1;
        FileInputStream v2 = null;
        bm v0 = null;
        if (!this.c.exists()) {
            return v0;
        }

        try {
            v2 = new FileInputStream(this.c);
            byte[] v3 = bw.b(((InputStream) v2));
            v1_2 = new bm();
            new cj().a(((cg) v1_2), v3);

            return v1_2;
        } catch (Exception v1_1) {
            v1_1.printStackTrace();
        } finally {
            bw.c(v2);
        }
        return v1_2;
    }
}

